import SwiftUI
import SPGlassKit

public struct GlassLocationButtonExample: GlassExample {
    public static let componentID = "glass-location-button"
    /// The example plays the host's role with a simulated state: no permission is requested, no position is read.
    @State private var state = GlassLocationState.idle
    @State private var message = "Sample data · simulated states, no location access"

    public init() {}

    public var body: some View {
        VStack(spacing: 16) {
            SampleMapBackdrop()
                .overlay(alignment: .bottomTrailing) {
                    GlassLocationButton(state: state, hint: hint) { advance() }
                        .padding()
                }
            Picker("Simulated state", selection: $state) {
                ForEach(GlassLocationState.allCases, id: \.self) { state in
                    Text(GlassLocationPresentation(state).label).tag(state)
                }
            }
            .padding(.horizontal)
            HStack(spacing: 12) {
                ForEach(GlassLocationState.allCases, id: \.self) { state in
                    GlassLocationButton(state: state, size: .small) {}
                }
            }
            Text(message)
                .font(.caption)
                .foregroundStyle(.secondary)
                .padding(.horizontal)
        }
        .padding(.bottom)
    }

    private var hint: String? {
        switch state {
        case .denied: "Location access was refused. Activating the button would open Settings in a real application."
        case .unavailable: "Location services are off on this device."
        default: nil
        }
    }

    /// What a host might do on a tap, simulated.
    private func advance() {
        switch state {
        case .idle:
            state = .locating
            message = "The host would start location updates here."
        case .locating:
            state = .following
            message = "A position arrived: the map follows it."
        case .following:
            state = .idle
            message = "Following stopped."
        case .denied:
            message = "The host would open Settings here."
        case .unavailable:
            break
        }
    }
}

#Preview("GlassLocationButton") { GlassLocationButtonExample().glassTheme(.emerald) }
